package com.wanqing.controller.user;

import com.wanqing.constant.StatusConstant;
import com.wanqing.entity.BookList;
import com.wanqing.result.Result;
import com.wanqing.service.BookListService;
import com.wanqing.vo.BookItemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userBookListController")
@RequestMapping("/user/bookList")
@Tag(name = "C端-书单浏览接口")
public class BookListController {
    @Autowired
    private BookListService bookListService;

    /**
     * 条件查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "根据分类id查询书单")
    @Cacheable(cacheNames = "bookListCache", key = "#categoryId") //key: bookListCache::categoryId
    public Result<List<BookList>> list(@RequestParam(defaultValue = "0") Long categoryId) {
        BookList bookList = new BookList();
        bookList.setCategoryId(categoryId);
        bookList.setStatus(StatusConstant.ENABLE);

        List<BookList> list = bookListService.list(bookList);
        return Result.success(list);
    }

    /**
     * 根据书单id查询包含的图书列表
     *
     * @param id
     * @return
     */
    @GetMapping("/book/{id}")
    @Operation(summary = "根据书单id查询包含的图书列表")
    public Result<List<BookItemVO>> bookList(@PathVariable("id") Long id) {
        List<BookItemVO> list = bookListService.getBookItemById(id);
        return Result.success(list);
    }
}